﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RpcGenerator.CSharp;

namespace RpcGenerator
{
    class RpcGenerator
    {
        static Dictionary<Type, MessageFieldDescription> allSharedObject = new Dictionary<Type, MessageFieldDescription>();
        public void Generator(Type type, string path,MessageFormatType format)
        {
            MessageDescriptionGenerator mdgenerator = new MessageDescriptionGenerator();
            List<MessageDescription> allDescriptions = mdgenerator.Generate(type);

            foreach (MessageDescription md in allDescriptions)
            {
                if (null != md.SharedObjects)
                {
                    foreach (MessageFieldDescription mfd in md.SharedObjects)
                    {
                        if (!allSharedObject.ContainsKey(mfd.Type))
                        {
                            allSharedObject[mfd.Type] = mfd;
                        }
                    }
                }
            }

            FileGenerator.GenerateMessageInterfaceProxyAndInvoker(type, allDescriptions, path,format);
        }

        public static void GenerateShareField(string path, MessageFormatType format)
        {
            foreach (MessageFieldDescription mfd in allSharedObject.Values)
            {
                FileGenerator.GenerateShareMessage(mfd, path, format);
            }
        }
    }
}
